package tech.aistar.corejava.day10.homework;

import java.util.Scanner;

/**
 * Created by 李雨昂 on 2019/7/27.
 * 找两个字符串的最大公共子序列
 */
public class Demo10 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("第一个字符串:>");
        String str1 = sc.nextLine();
        System.out.println("第二个字符串:>");
        String str2 = sc.nextLine();

        printCommon(str1,str2);

    }
    private static void printCommon(String str1, String str2) {
        int len = 0;//上一个子串的长度
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < str2.length(); i++) {
            for (int j = i+1; j < str2.length(); j++) {

                //截取 - 将第二个字符串的所有的子串全部找出来.
                String substr = str2.substring(i,j);

                //获取子串的长度.
                int sublen = substr.length();

                //判断这些子串是否在str1中存在.
                if(str1.contains(substr)  && sublen >len){
                    //清空builder
                    builder.delete(0,builder.capacity());
                    builder.append(substr+" ");

                    //把当前子串的长度给len
                    len = sublen;
                }else if(str1.contains(substr) && sublen == len){
                    builder.append(substr);
                }
            }
        }
        System.out.println(builder);
    }

}
